
from django.shortcuts import render_to_response
from django.http import HttpResponse,  HttpResponseRedirect
from django.contrib.auth import logout
from django.template import RequestContext
from market.models import  BetRoom, Player
from market.authentification import authentificatePlayer, authentificateUser, authentificateUserWithBetroom
from market.stupidUserException import StupidUserException
from django.utils import simplejson
from django.db import transaction
import traceback
import sys

#######################################################################################
# when a request is moifying the data base it is fine to cal the function which is modifying it through this interface
# since it will collect the exception, save the base in case of success and render a html response in case the request
# has failled so the exception can be display in the browser
@transaction.commit_on_success
def handleRequestWithException( funcToCall, arg1, userOrPlayerOrBetroom ):
        sid = transaction.savepoint()
        try:
                tmp = funcToCall( arg1, userOrPlayerOrBetroom )
                if tmp == 0:
                        return HttpResponse( "0" )
                return tmp
        except StupidUserException, e:
                transaction.savepoint_rollback(sid)
                return HttpResponse( "Hem hem: " + str(e) )
        except:
                transaction.savepoint_rollback(sid)
                raise
        

#######################################################################################
# check the player is logged, check for POST method and call the function
def handleRequestUserPOSTWithException( funcToCall, request ):
        # reject non POST request
	if request.method != 'POST':
                return HttpResponseRedirect( request.path )

        # authentification
        alternative_response_list = [] #the answer to return if the player could not be authentificated with a dummy initialisation
        if not( authentificateUser( request, alternative_response_list ) ):
                return alternative_response_list[0]

        return handleRequestWithException( funcToCall, request, request.user )


#######################################################################################
# check the player is logged, check for POST method and call the function when the bet are not suspended
def handleRequestPlayerPOSTWithExceptionWhenNotSuspended( funcToCall, request ):
        # reject non POST request
	if request.method != 'POST':
                return HttpResponseRedirect( request.path )

        # authentification
        player_list = [] #the player with a dummy initialisation
        alternative_response_list = [] #the answer to return if the player could not be authentificated with a dummy initialisation
        if not( authentificatePlayer( request, player_list, alternative_response_list ) ):
                return alternative_response_list[0]
                
        if player_list[0].dbPlayer.mainStrategy.betroom.status == 2:
                return HttpResponse( 'transactions are suspended' )   
                
        
        return handleRequestWithException( funcToCall, request, player_list[0] )
                
                
#######################################################################################
# check the player is logged, check for POST method and call the function when the bet are not suspended
def handleRequestBetroomAdminPOSTWithException0( funcToCall, request, checkAdmin ):
        
        # reject non POST request
	if request.method != 'POST':
                return HttpResponseRedirect( request.path )
        # authentification
        betroom =[]
        alternative_response_list = [] #the answer to return if the player could not be authentificated with a dummy initialisation
        if not( authentificateUserWithBetroom( request, betroom, alternative_response_list ) ):
                return alternative_response_list[0]

        if checkAdmin:
                if betroom[0].personInCharge != request.user:
                        raise Exception("You have to be in charge of the betroom to perform this action")

        return handleRequestWithException( funcToCall, request, betroom[0] )
              
              
#######################################################################################
# check the user is logged and the admin of the betroom, check for POST method and call the function when the bet are not suspended
def handleRequestBetroomAdminPOSTWithException( funcToCall, request ):
        return handleRequestBetroomAdminPOSTWithException0( funcToCall, request, True )
        
#######################################################################################
# check the user is logged, check for POST method and call the function when the bet are not suspended
def handleRequestBetroomPOSTWithException( funcToCall, request ):
        return handleRequestBetroomAdminPOSTWithException0( funcToCall, request, False )

        

                  
